package cn.thornbird.orgsync.repository;

import cn.thornbird.orgsync.entity.CertificateDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CertificateDetailRepository
        extends BaseRepository<CertificateDetail>, JpaRepository<CertificateDetail, String> {

    List<CertificateDetail> findAllByCertificateIdOrderByAppType(String certificateId);

    @Query(nativeQuery = true, value = "select * from certificate_details d left outer join certificates c " +
            "on d.certificate_id = c.id where c.user_id = :userId and d.deleted_at is null " +
            "order by d.certificate_id asc, d.app_type asc")
    List<CertificateDetail> findAllByUserId(String userId);

    @Modifying
    @Query("update #{#entityName} e set e.deletedAt = CURRENT_TIMESTAMP where e.certificateId = ?1")
    void logicDeleteRecords(String certificateId);

}
